﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using AjaxPro;

public partial class Samples_TestStockFormList : System.Web.UI.Page
{
    #region 伪造模拟的数据
    private static DataSet _dsStockFormData;
    public static DataSet DsStockFormData
    {
        get { 
            if(_dsStockFormData==null)
                initStockFormData();
            return Samples_TestStockFormList._dsStockFormData; }
    }
    private static void initStockFormData()
    {
        _dsStockFormData = new DataSet();
        //虚拟一个入库单表
        DataTable flist = new DataTable("StockFormList");
        flist.Columns.Add("FormId", System.Type.GetType("System.String"));
        flist.Columns.Add("FormCaption", System.Type.GetType("System.String"));
        flist.Columns.Add("ProductTNumer", System.Type.GetType("System.Int32"));
        flist.Columns.Add("ProductTNumPrice", System.Type.GetType("System.Single"));
        flist.Columns.Add("CreatePerson", System.Type.GetType("System.String"));
        flist.Columns.Add("CreateDate", System.Type.GetType("System.DateTime"));
        flist.Columns.Add("FormState", System.Type.GetType("System.Int32"));
        _dsStockFormData.Tables.Add(flist);
        //虚拟一个入库单明细表
        DataTable dlist = new DataTable("StockFormListDetail");
        dlist.Columns.Add("ProductId", System.Type.GetType("System.String"));
        dlist.Columns.Add("FormId", System.Type.GetType("System.String"));
        dlist.Columns.Add("ProductName", System.Type.GetType("System.String"));
        dlist.Columns.Add("ProductNumber", System.Type.GetType("System.Int32"));
        dlist.Columns.Add("ProductPrice", System.Type.GetType("System.Single"));
        dlist.Columns.Add("ProductCode", System.Type.GetType("System.String"));
        dlist.Columns.Add("ProductState", System.Type.GetType("System.Int32"));
        _dsStockFormData.Tables.Add(dlist);

        initData();

    }
    private static void initData()
    {
        for (int i = 0; i < 50; i++)
        {
            string id=Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "");
            int tnum=new Random(i).Next(1, 10);
            float tprice = 0;
            for (int j = 0; j < tnum; j++)
            {
                DataRow dr = _dsStockFormData.Tables["StockFormListDetail"].NewRow();
                dr["ProductId"] = i+"_"+j.ToString();
                dr["FormId"] = id;
                dr["ProductName"] = "测试商品_" + dr["ProductId"].ToString();
                dr["ProductNumber"] = new Random(j).Next(1, 100);
                dr["ProductPrice"] = (float)new Random(j ).Next(1, 10000) / 10;
                dr["ProductCode"] = "测试编码_" + j.ToString();
                dr["ProductState"] = new Random(j ).Next(0, 4);

                tprice += (float)dr["ProductPrice"];
                _dsStockFormData.Tables["StockFormListDetail"].Rows.Add(dr);
                
            }

            DataRow dr1 = _dsStockFormData.Tables["StockFormList"].NewRow();

            dr1["FormId"] = id;
            dr1["FormCaption"] = "测试单据_" + i.ToString();
            dr1["ProductTNumer"] = tnum;
            dr1["ProductTNumPrice"] = tprice;
            dr1["CreatePerson"] = "超级用户" + new Random(i).Next(0, 3);
            dr1["CreateDate"] = DateTime.Now;
            dr1["FormState"] = new Random(i).Next(1, 3);

            _dsStockFormData.Tables["StockFormList"].Rows.Add(dr1);
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Samples_TestStockFormList));
        initStockFormData();
        initGrid1();
        initGrid2();
    }

    
    public void initGrid1()
    {
        this.dotNetFlexGrid1.InitConfig(
            new string[]{
                "title=入库单列表",//标题
                "striped=true",//是否显示行交替色
                "selectedonclick=true",//是否点击行自动选中checkbox
                "usepager=true",//使用分页器
                "showcheckbox=true",//显示复选框
                "singleselected=true",//是否单选
                "height=200",//高度，可为auto或具体px值
                "width=680"//宽度，可为auto或具体px值
            },
            new dotNetFlexGrid.FieldConfig[]{
                new dotNetFlexGrid.FieldConfig("FormId","编号",60,true,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("FormCaption","标题",120,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductTNumer","合计数量",60,true,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductTNumPrice","合计价格",60,true,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("CreatePerson","创建人",80,true,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("CreateDate","创建日期",120,true,dotNetFlexGrid.FieldConfigAlign.Center),
                new dotNetFlexGrid.FieldConfig("FormState","状态码",60,true,dotNetFlexGrid.FieldConfigAlign.Left,false,true,true),
                new dotNetFlexGrid.FieldConfig("FormStateText","状态",60,true,dotNetFlexGrid.FieldConfigAlign.Left)
            }
        );
        dotNetFlexGrid1.DataHandler = new dotNetFlexGrid.DataHandlerDelegate(DotNetFlexGrid1DataHandler);
    }
    public dotNetFlexGrid.DataHandlerResult DotNetFlexGrid1DataHandler(dotNetFlexGrid.DataHandlerParams p)
    {
        dotNetFlexGrid.DataHandlerResult result = new dotNetFlexGrid.DataHandlerResult();
        result.page = p.page;//设定当前返回的页号
        result.table = DsStockFormData.Tables["StockFormList"];
        result.total = result.table.Rows.Count;//总计的数据条数
        
        //如果传递的参数包含附加参数的话，再来模拟查询，原谅我真的很懒。
        if (p.extParam.ContainsKey("FormState"))
        {
            if (p.extParam["FormState"] != "0")
            {
                System.Data.DataView dv = result.table.DefaultView;
                dv.RowFilter = "FormState = " + p.extParam["FormState"] + "";
                result.table = dv.ToTable();
                result.total = result.table.Rows.Count;//总计的数据条数
            }
        }
        //如果传递的参数包含排序设置的话，通过DataView.Sort功能模拟进行当页排序
        if (p.sortname.Length > 0 && p.sortorder.Length > 0)
        {
            System.Data.DataView dv = result.table.DefaultView;
            if(p.sortname=="FormStateText")//这个字段是格式化列，虚拟的，故需要转发到对应的列进行处理
                dv.Sort = ("FormState " + p.sortorder);
            else
                dv.Sort = (p.sortname + " " + p.sortorder);
            result.table = dv.ToTable();
        }
        //最后进行分页处理
        if (p.rp > 0)
        {
            DataTable newDt = result.table.Clone();
            for (int i = (p.page - 1) * p.rp; i < (p.page) * p.rp; i++)
            {
                if (i < result.table.Rows.Count - 1)
                {
                    newDt.ImportRow(result.table.Rows[i]);
                }
                else
                {
                    break;
                }
            }
            result.table = newDt;
        }
        //提供格式化处理
        dotNetFlexGrid.FieldFormatorHandle proc = delegate(DataRow dr)
        {
            switch (dr["FormState"].ToString().ToLower())
            {
                case "1":
                    return "<b><span style='color:Blue'>已入库</span></b>";
                case "2":
                    return "<b><span style='color:Gray'>草稿</span></b>";
            }
            return "";
        };
        result.FieldFormator.Register("FormStateText", proc);
        return result;
    }
    public void initGrid2()
    {
        this.dotNetFlexGrid2.InitConfig(
            new string[]{
                "title=商品清单",//标题
                "striped=true",//是否显示行交替色
                "selectedonclick=true",//是否点击行自动选中checkbox
                "usepager=false",//使用分页器
                "showcheckbox=false",//显示复选框
                "height=100",//高度，可为auto或具体px值
                "width=680"//宽度，可为auto或具体px值
            },
            new dotNetFlexGrid.FieldConfig[]{
                new dotNetFlexGrid.FieldConfig("ProductId","编号",60,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductName","商品名",100,false,dotNetFlexGrid.FieldConfigAlign.Center),
                new dotNetFlexGrid.FieldConfig("ProductNumber","数量",50,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductPrice","单价",50,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductCode","批号",60,false,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("ProductState","状态",50,false,dotNetFlexGrid.FieldConfigAlign.Left),
            }
        );
        dotNetFlexGrid2.DataHandler = new dotNetFlexGrid.DataHandlerDelegate(DotNetFlexGrid2DataHandler);
    }
    public dotNetFlexGrid.DataHandlerResult DotNetFlexGrid2DataHandler(dotNetFlexGrid.DataHandlerParams p)
    {
        dotNetFlexGrid.DataHandlerResult result = new dotNetFlexGrid.DataHandlerResult();
        DataSet ds = Samples_TestStockFormList.DsStockFormData;
        string query = string.Empty;
        if (p.extParam.ContainsKey("formId"))
        {
            query = "FormId='" + p.extParam["formId"] + "'";
        }
        else
        {
            query = "FormId='-1'";//不是表格1联动而来，偷下懒，用个不存在的查询
        }
        //模拟查询，实际应用要根据实际情况替换为数据库查询
        DataRow[] drs = ds.Tables["StockFormListDetail"].Select(query);
        DataTable dt = ds.Tables["StockFormListDetail"].Clone();
        foreach (DataRow dr in drs)
            dt.ImportRow(dr);

        result.page = p.page;//设定当前返回的页号
        result.total = dt.Rows.Count;//总计的数据条数，
        result.table = dt;//调用演示的数据生成函数产生模拟数据
        //格式化ProductState字段
        dotNetFlexGrid.FieldFormatorHandle proc = delegate(DataRow dr)
        {
            switch (dr["ProductState"].ToString().ToLower())
            {
                case "0":
                    return "<span style='color:red'>禁用</span>";
                case "1":
                    return "<span style='color:Blue'>下架</span>";
                case "2":
                    return "<span style='color:Gray'>正常销售</span>";
                case "3":
                    return "已售空";
            }
            return "";
        };
        //格式化ProductPrice字段
        dotNetFlexGrid.FieldFormatorHandle proc1 = delegate(DataRow dr)
        {
            return "￥" + dr["ProductPrice"].ToString();
        };
        result.FieldFormator.Register("ProductState", proc);
        result.FieldFormator.Register("ProductPrice", proc1);

        return result;
    }
    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
    public bool DeleteStockForm(string formId)
    {
        bool result = false;
        if (formId != null && formId.Length > 0)
        {
            DataRow[] drs=DsStockFormData.Tables["StockFormList"].Select("FormId='"+formId+"'");
            if (drs != null && drs.Length > 0)
                DsStockFormData.Tables["StockFormList"].Rows.Remove(drs[0]);
            drs = DsStockFormData.Tables["StockFormListDetail"].Select("FormId='" + formId + "'");
            if (drs != null && drs.Length > 0)
            foreach(DataRow dr in drs)
                DsStockFormData.Tables["StockFormListDetail"].Rows.Remove(dr);
            result=true;
        }
        return result;
    }
}
